﻿Public Class frmDefectSelect

    Private Const ITEM_PER_PAGE As Integer = 30
    Private Const ITEM_PER_ROW As Integer = 3
    Private Const ITEM_PER_COL As Integer = 10
    Public htMsgMst As New Hashtable

    Public Structure Defect
        Dim szCode As String
        Dim szName As String
    End Structure

    Private dtDefectCata As New DataTable
    Private Shared dfSelect As Defect
    Private btnLast As New Button

    Public WriteOnly Property DefectCata As DataTable
        Set(ByVal value As DataTable)
            dtDefectCata = value
        End Set
    End Property

    Public Property SelectedCata As Defect
        Get
            SelectedCata = dfSelect
        End Get
        Set(ByVal value As Defect)
            dfSelect = value
        End Set
    End Property

    Private m_iCount As Integer

    Private Sub frmDefectSelect_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        m_iCount = dtDefectCata.Rows.Count
        If m_iCount <= 0 Then
            MsgBox(htMsgMst("MSG00081"))
            'Me.Close()
        End If

        btnOK.DialogResult = Windows.Forms.DialogResult.OK
        btnCancel.DialogResult = Windows.Forms.DialogResult.Cancel
        btnGood.DialogResult = Windows.Forms.DialogResult.Ignore

        If Not String.IsNullOrEmpty(dfSelect.szCode) Then
            lblSelect.Text = String.Format("{0}:{1}", dfSelect.szCode, dfSelect.szName)
        End If
        ShowCata()
    End Sub

    Private Sub ShowCata()
        Dim btnCata() As Button = New Button(m_iCount - 1) {}
        Dim iSpaceX, iSpaceY As Integer
        iSpaceX = Int(tab1.Width / ITEM_PER_ROW)
        iSpaceY = Int(tab1.Height / ITEM_PER_COL)

        Dim iPageCnt As Integer = Int(m_iCount / ITEM_PER_PAGE)
        Dim iPage As Integer
        Dim i As Integer
        Dim newFont As Font = New Font("NEW_FONT", 16, FontStyle.Bold)

        Dim iSelect As Integer = -1
        For iPage = 0 To iPageCnt
            If iPage >= 1 Then
                TabControl.TabPages.Add("tab" & (iPage + 1).ToString)
                With TabControl.TabPages(iPage)
                    .Text = "Page" & (iPage + 1)
                    .BackColor = Drawing.SystemColors.Control
                End With
            End If

            Dim iPevCnt As Integer = ITEM_PER_PAGE * iPage
            For i = 0 To ITEM_PER_PAGE - 1
                If i + iPevCnt >= m_iCount Then
                    If iSelect >= 0 Then
                        btnCata(iSelect).BackColor = Color.DodgerBlue
                        btnLast = btnCata(iSelect)
                        TabControl.SelectedTab = CType(btnCata(iSelect).Parent, TabPage)
                        lblSelect.Text = String.Format("{0}:{1}", dfSelect.szCode, btnCata(iSelect).Text)
                        dfSelect.szName = btnCata(iSelect).Text
                    End If
                    Exit Sub
                End If
                btnCata(i + iPevCnt) = New Button
                With btnCata(i + iPevCnt)
                    .Name = "btnCata" & i.ToString
                    .Text = dtDefectCata.Rows(i + iPevCnt)("不良内容").ToString
                    .Tag = dtDefectCata.Rows(i + iPevCnt)("コード").ToString
                    .Width = iSpaceX - 6
                    .Height = iSpaceY - 6
                    .Left = 3 + iSpaceX * (i Mod ITEM_PER_ROW)
                    .Top = 3 + iSpaceY * Int(i / ITEM_PER_ROW)
                    .BackColor = Color.Red
                    .Font = newFont

                    If Not String.IsNullOrEmpty(lblSelect.Text) Then
                        If .Tag.Equals(dfSelect.szCode) Then
                            iSelect = i + iPevCnt
                        End If
                    End If
                End With
                AddHandler btnCata(i + iPevCnt).Click, AddressOf btnCata_Click
                TabControl.TabPages(iPage).Controls.Add(btnCata(i + iPevCnt))
            Next
        Next
        If iSelect >= 0 Then
            btnCata(iSelect).BackColor = Color.DodgerBlue
            btnLast = btnCata(iSelect)
            TabControl.SelectedTab = CType(btnCata(iSelect).Parent, TabPage)
            lblSelect.Text = String.Format("{0}:{1}", dfSelect.szCode, btnCata(iSelect).Text)
            dfSelect.szName = btnCata(iSelect).Text
        End If
    End Sub

    Private Sub btnCata_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim btnCata As Button = CType(sender, Button)

        lblSelect.Text = String.Format("{0}:{1}", btnCata.Tag, btnCata.Text)
        dfSelect.szCode = btnCata.Tag
        dfSelect.szName = btnCata.Text
        btnLast.BackColor = Color.Red
        btnCata.BackColor = Color.DodgerBlue
        btnLast = btnCata
    End Sub
End Class